package com.why.blog.service.iml;

import com.why.blog.bean.Blog;
import com.why.blog.dao.blogDao;
import com.why.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: blog
 * @description:
 * @author: @why
 * @create: 2021-01-16 12:00
 **/
@Slf4j
@Service
public class blogServiceIml implements BlogService {

    @Resource
    private blogDao blog;

    @Override
    public List<Blog> getListBlog() {
        return blog.getListBlog();
    }

    @Override
    public Blog getBlogById(Integer id) {
        return blog.getBlogById(id);
    }

    @Override
    public List<Blog> getListBlogByType(Integer typeId) {
        return blog.getListBlogByType(typeId);
    }

    @Transactional
    @Override
    public int updateBlog(Blog blog) {

        blog.setUpdate_time(new Date());
        return this.blog.updateBlog(blog);
    }

    @Transactional
    @Override
    public int deleteBlogById(Integer id) {
        return this.blog.deleteBlogById(id);
    }

    @Transactional
    @Override
    public void saveBlog(Blog blog) {

        blog.setUser_id(1);
        blog.setViews(1);
        blog.setUpdate_time(new Date());
        blog.setCreate_time(new Date());
        this.blog.saveBlog(blog);

    }

    @Override
    public int blogNums() {
        return blog.blogNums();
    }

    @Override
    public List<Blog> getListBlogNoContent() {
        return blog.getListBlogNoContent();
    }

    @Override
    public  Map<String, List<Blog>> getBlogByYear() {
        //得到所有的年份
        List<String> year = blog.getYear();
        //按年份分类，存入map,别用HashMap，
        Map<String, List<Blog>> yearOfBlog = new LinkedHashMap<>();
        for (String y : year) {
            yearOfBlog.put(y, blog.getBlogByYear(y));
        }
        return yearOfBlog;
    }

    @Override
    public List<Blog> getBlogByName(String name) {
        String processName="%"+name+"%";

        return  blog.getBlogByName(processName);
    }

    @Transactional
    @Override
    public void blogViewsAutoIncrement(Integer blogId) {
        this.blog.blogViewsAutoIncrement(blogId);
    }

}


